﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MySql.Data.MySqlClient;
using MySql.Data.Types;

namespace EP2_Gameserver
{
    class Packages
    {
        public static MemoryStream ShowCharacters(int CharacterID)
        {


            string strProvider = "Data Source=" + mySQL.host + ";Database=" + mySQL.database + ";User ID=" + mySQL.user + ";Password=" + mySQL.password;
            MySqlConnection mysqlCon = new MySqlConnection(strProvider);
            mysqlCon.Open();

            MySqlCommand verifyUser = new MySqlCommand("SELECT * FROM t_characters WHERE a_index = "+ CharacterID.ToString() + ";", mysqlCon);
            verifyUser.ExecuteNonQuery();

            MySqlDataReader myReader = verifyUser.ExecuteReader();

            int[] id = new int[100];


            myReader.Read();



            



            int CharID = Convert.ToInt32(myReader.GetValue(0));
            string CharName = myReader.GetValue(2).ToString();
            byte CharClass = Convert.ToByte(myReader.GetValue(3));
            byte CharHair =  Convert.ToByte(myReader.GetValue(5));
            byte CharJob = 2;
            byte CharFace = Convert.ToByte(myReader.GetValue(4));
            int CharLevel = Convert.ToInt32(myReader.GetValue(6));
            Int64 CurrentEXP = Convert.ToInt64(myReader.GetValue(15)); //15
            Int64 MaximumEXP = Convert.ToInt64(myReader.GetValue(16)); // 16
            int CurrentHP = Convert.ToInt32(myReader.GetValue(7)); //7
            int MaximumHP = Convert.ToInt32(myReader.GetValue(8)); //8
            int CurrentMP = Convert.ToInt32(myReader.GetValue(9)); //9
            int MaximumMP = Convert.ToInt32(myReader.GetValue(10)); //10
            int SP = 0; // Convert.ToInt32(myReader.GetValue(11)); //11
            
            // 19 = id's
            // 20 = upgrades

            string[] rawitemID = myReader.GetValue(19).ToString().Split(' ');
            int[] ItemID = new int[8];
            for( int a = 0; a <= 7 ; a++ ) 
            {
                ItemID[a] = Convert.ToInt32( rawitemID[a]);
            }

            string[] rawplus = myReader.GetValue(20).ToString().Split(' ');
            int[] ItemPlus = new int[8];
            for (int a = 0; a <= 7; a++)
            {
                ItemPlus[a] = Convert.ToInt32(rawplus[a]);
            }

            int Helmet_ID = ItemID[0];
            int Helmet_Upgrade = ItemPlus[0];
            int Shirt_ID = ItemID[1];
            int Shirt_Upgrade = ItemPlus[1];
            int Weapon_ID = ItemID[5];
            int Weapon_Upgrade = ItemPlus[5];
            int Pants_ID = ItemID[2];
            int Pants_Upgrade = ItemPlus[2];
            int Shield_ID = ItemID[6];
            int Shield_Upgrade = ItemPlus[6];
            int Gloves_ID = ItemID[3];
            int Gloves_Upgrade = ItemPlus[3];
            int Boots_ID = ItemID[4];
            int Boots_Upgrade = ItemPlus[4];
            int Wing_ID = ItemID[7];
            int Wing_Upgrade = ItemPlus[7];
            int CharDeletedTime = 0;

            MemoryStream td = new MemoryStream();
            td.AddByte(0x02);
            td.AddByte(0x02);
            
            td.AddInt32(CharID);
            td.AddString(CharName);
            td.AddByte(0x00);
            td.AddByte(CharClass);
            td.AddByte(CharJob);
            td.AddByte(CharHair);
            td.AddByte(CharFace);
            td.AddInt32(CharLevel);
            td.AddInt64(CurrentEXP);
            td.AddInt64(MaximumEXP);
            td.AddInt32(SP);
            td.AddInt32(MaximumHP);
            td.AddInt32(CurrentHP);
            td.AddInt32(MaximumMP);
            td.AddInt32(CurrentMP);
            td.AddInt32(Helmet_ID);
            td.AddInt32(Helmet_Upgrade);
            td.AddInt32(Shirt_ID);
            td.AddInt32(Shirt_Upgrade);
            td.AddInt32(Weapon_ID);
            td.AddInt32(Weapon_Upgrade);
            td.AddInt32(Pants_ID);
            td.AddInt32(Pants_Upgrade);
            td.AddInt32(Shield_ID);
            td.AddInt32(Shield_Upgrade);
            td.AddInt32(Gloves_ID);
            td.AddInt32(Gloves_Upgrade);
            td.AddInt32(Boots_ID);
            td.AddInt32(Boots_Upgrade);
            td.AddInt32(Wing_ID);
            td.AddInt32(Wing_Upgrade);
            td.AddInt32(CharDeletedTime);
            td.AddInt32(0);
            td.AddByte(0x00);

            return td;

        }

        public static MemoryStream GoCharSelectScreen()
        {
            MemoryStream td = new MemoryStream();
            td.AddByte(0x02);
            td.AddByte(0x03);

            return td;

        }

        public static byte[] CharSelectedLoadMap()
        {
            byte[] pack = {0x02,0x01};
            return pack;
        }

        public static MemoryStream SendChatMessage(int CharID, string CharName, string Message, byte MsgType)
        {

            MemoryStream td = new MemoryStream();
            td.AddByte(0x0F);
            td.AddByte(MsgType);
            td.AddInt32r(CharID);
            td.AddString(CharName);
            td.AddByte(0x00);
            td.AddByte(0x00);
            td.AddString(Message);
            td.AddByte(0x00);
            td.AddInt32(-1);
            td.AddByte(0x00);
            td.AddByte(0x00);

            return td;

        }


        public static MemoryStream EnterWorldPack1()
        {
            int CharID = 861;
            string CharName = "Wizatek";
            byte CharClass = 0x04; // Rogue
            byte CharUnknown1 = 0x00;
            byte CharHair = 0x02;
            byte CharFace = 0x01;
            int dunno1 = 0;
            int dunno2 = 100;
            float LocX = 1115;
            float LocZ = 990;
            float LocH = 161;
            float LocR = 0;
            byte[] msg = { 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x3a, 0xe2, 0x01, 0x00, 0x00, 0x00, 0x06, 0x7e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };


            MemoryStream td = new MemoryStream();
            td.AddByte(0x09);
            td.AddInt32(CharID);
            td.AddString(CharName);
            td.AddByte(0x00);
            td.AddByte(CharClass);
            td.AddByte(CharUnknown1);
            td.AddByte(CharHair);
            td.AddByte(CharFace);
            td.AddInt32(dunno1);
            td.AddInt32(dunno2);
            td.AddFloat(LocX);
            td.AddFloat(LocZ);
            td.AddFloat(LocH);
            td.AddFloat(LocR);
            td.AddBytes(msg);

            return td;

        }

        public static MemoryStream GameTime()
        {

            int year = 2;
            byte month = 3;
            byte day = 4;
            byte hour = 5;

            MemoryStream td = new MemoryStream();
            td.AddByte(0x16);
            td.AddByte(0x02);
            td.AddInt32(year);
            td.AddByte(month);
            td.AddByte(day);
            td.AddByte(hour);
            td.AddByte(0x00);

            return td;

        }

        public static MemoryStream ThisPlayerGuildInfo()
        {
            int GuildID = 1;
            string GuildName = "trert";
            int GuildLevel = 1;

            MemoryStream td = new MemoryStream();
            td.AddByte(0x2a);
            td.AddByte(0x02);
            td.AddInt32(GuildID);
            td.AddString(GuildName);
            td.AddByte(0x00);
            td.AddInt32(GuildLevel);
            td.AddInt32(0);
            td.AddInt32(0);
            td.AddByte(0x00); // Guild color ( yellow / red) ?
            td.AddByte(0x00); // Guild frame ?
            td.AddByte(0x00);
            td.AddByte(0x00);
            td.AddByte(0x00);
            td.AddByte(0x00);
            td.AddByte(0x00);
            td.AddByte(0x00);


            return td;

        }

        public static MemoryStream ThisPlayerGuildMembers()
        {
            int GuildID = 1;
            string PlayerName = "[GM]Jantje";
            byte GuildMembers = 1;
            int PlayerID = 861;
            int PlayerGuildMemberID = 1;

            MemoryStream td = new MemoryStream();
            td.AddByte(0x2a);
            td.AddByte(0x03);
            td.AddInt32(GuildID);
            td.AddInt32(PlayerGuildMemberID);
            td.AddInt32(PlayerID);
            td.AddString(PlayerName);
            td.AddByte(0x00);
            td.AddInt32(0);
            td.AddByte(GuildMembers);
            td.AddByte(0x00);
            td.AddByte(0x00);
            td.AddByte(0x00);
            td.AddByte(0x00);
            td.AddByte(0x00);

            return td;

        }

        public static MemoryStream ThisPlayerInfo()
        {
            int PlayerLevel = 100;
            Int64 CurrentEXP = 10;
            Int64 MaximumEXP = 100;
            int CurrentHP = 1000;
            int MaxNormalHP = 1000;
            int MaxBoostedHP = 1000; // I think its like this the hp with hp boosts and buffs and such
            int CurrentMP = 1000;
            int MaxNormalMP = 1000;
            int MaxBoostedMP = 1000;
            int Strength = 0;
            int Dexterity = 0;
            int Intelligence = 0;
            int Constitution = 0;
            int EQStrength = 0; // total stats from only the equipment / buffs
            int EQDexterity = 0;
            int EQIntelligence = 0;
            int EQConstitution = 0;
            int PhysicalAttack = 100;
            Int64 MagicAttack = 50;
            Int64 PhysicalDefense = 100;
            Int64 MagicDefense = 10;
            Int64 PhysicalEvasion1 = 100;
            int PhysicalEvasion2 = 100;
            int MagicEvasion1 = 50;
            int MagicEvasion2 = 50;
            int PhysicalHitrate1 = 1000;
            int PhysicalHitrate2 = 1000;
            int MagicHitrate1 = 10;
            int MagicHitrate2 = 10;
            int Critical1 = 2000;
            int Critical2 = 2000;
            int Deadly1 = 200;
            int Deadly2 = 200;
            int SP = 20000; // = 2sp  
            int NoIdea1 = 17110;
            int NoIdea2 = 14360;
            int NoIdea3 = 49215;
            float MovementSpeed = 19.9f;
            float OtherSpeed = 6.5f;
            byte AttackSpeed = 5;
            byte AttackSpeed2 = 9;
            byte NoIdea31 = 110;
            byte NoIdea32 = 255;
            int GoodEvil = 1555;
            int PKValue = 110;
            int Reputation = 300;
            float AttackRange = 0.0f;
            int NoIdea8 = 0;
            int NoIdea9 = 0;
            int NoIdea10 = 0;
            int NoIdea11 = 0;
            int NoIdea12 = 0;
            int NoIdea13 = 0;
            byte Ignition_Level = 1;
            int NoIdea15 = 0;

            byte[] NoIdea17 = { 0x57, 0x30, 0x34, 0x10 };

            MemoryStream td = new MemoryStream();
            td.AddByte(0x06);
            td.AddInt32(PlayerLevel);
            td.AddInt64(CurrentEXP);
            td.AddInt64(MaximumEXP);
            td.AddInt32(CurrentHP);
            td.AddInt32(MaxNormalHP);
            td.AddInt32(MaxBoostedHP);
            td.AddInt32(CurrentMP);
            td.AddInt32(MaxNormalMP);
            td.AddInt32(MaxBoostedMP);
            td.AddInt32(Strength);
            td.AddInt32(Dexterity);
            td.AddInt32(Intelligence);
            td.AddInt32(Constitution);
            td.AddInt32(EQStrength);
            td.AddInt32(EQDexterity);
            td.AddInt32(EQIntelligence);
            td.AddInt32(EQConstitution);
            td.AddInt32(PhysicalAttack);
            td.AddInt64(MagicAttack);
            td.AddInt64(PhysicalDefense);
            td.AddInt64(MagicDefense);
            td.AddInt64(PhysicalEvasion1);
            td.AddInt32(PhysicalEvasion2);
            td.AddInt32(MagicEvasion1);
            td.AddInt32(MagicEvasion2);
            td.AddInt32(PhysicalHitrate1);
            td.AddInt32(PhysicalHitrate2);
            td.AddInt32(MagicHitrate1);
            td.AddInt32(MagicHitrate2);
            td.AddInt32(Critical1);
            td.AddInt32(Critical2);
            td.AddInt32(Deadly1);
            td.AddInt32(Deadly2);
            td.AddInt32(SP);
            td.AddInt32(NoIdea1);
            td.AddInt32(NoIdea2);
            td.AddInt32(NoIdea3);
            td.AddFloat(MovementSpeed);
            td.AddFloat(OtherSpeed);
            td.AddByte(AttackSpeed);
            td.AddByte(AttackSpeed2);
            td.AddByte(NoIdea31);
            td.AddByte(NoIdea32);
            td.AddInt32(GoodEvil);
            td.AddInt32(PKValue);
            td.AddInt32(Reputation);
            td.AddFloat(AttackRange);
            td.AddInt32(NoIdea8);
            td.AddInt32(NoIdea9);
            td.AddInt32(NoIdea10);
            td.AddInt32(NoIdea11);
            td.AddInt32(NoIdea12);
            td.AddInt32(NoIdea13);
            td.AddByte(Ignition_Level);
            td.AddInt32(NoIdea15);
            td.AddBytes(NoIdea17);
            
            /*
            td.AddByte(0x00);
            td.AddByte(0x03);
            td.AddByte(0x5d);
            */
            

            return td;
        }

        public static MemoryStream InventoryMessage()
        {

            

            byte Type = 0; // 0=General Inventory 2=Event Inventory So i guess 1=Quest Inventory
            byte InventoryRow = 0;

            // Every Inventory row has its own package.
            // 5 Items per Row
            // Empty rows have no packet
            // Empty inventory slots in a row with filled slots will have ItemSerial (Int32 -1)
            //
            // Trying to figure out the structure of this packet with different items in the same slot (i could be wrong somewhere)
            //
            //
            /****************************************************************************************************************************************************************
            /*   Weapon Rare Option and Sockets *   Platinum Blessed Iris       *  Gloves Bloodseal able       * Weapon, Bloodseal Unable       * Accesoire with rare option
             *                                  *                               *  socket able, ES able        * ES able, Socket able           *
             * ***************************************************************************************************************************************************************
             * int32 - Item Serial              * int32 - Item Serial           * Int32 - Item Serial           * Int32 - Item Serial           * Int32 - Item Serial
             * int32 - Item ID                  * int32 - Item ID               * Int32 - Item ID               * Int32 - Item ID               * Int32 - Item ID
             * Byte - ? 4                       * byte - ? -1                   * byte - ? (-1)                 * byte - ? (-1)                 * byte - ? (-1)
             * int32 - Item Upgrade (15)        * int32 - Item Upgrade (0)      * Int32 - Upgrade               * Int32 - Upgrade               * Int32 - Upgrade
             * int16 - ? (4)                    * Int16 - ? (0)                 * Int16 - ? (0)                 * Int16 - (0)                   * Int16 - ? (0)
             * int16 - ?                        * Int16 - ? (0)                 * Int16 - ? (128)               * Int16 - (0)                   * Int16 - ? (0)
             * int32 - ? -1                     * Int32 - ? (-1)                * Int32 - ? (-1)                * Int32 - ? (-1)                * Int32 - ? (-1) 
             * int32 - ? -1                     * Int32 - ? (-1)                * Int32 - ? (-1)                * Int32 - ? (-1)                * Int32 - ? (-1)
             * int32 - ? -1                     * Int32 - ? (-1)                * Int32 - ? (-1)                * Int32 - ? (-1)                * Int32 - ? (-1)
             * int32 - ? 0                      * Int32 - ? (0)                 * Int32 - ? (0)                 * Int32 - ? (0)                 * Int32 - ? (0)
             * int32 -Item Quantity             * Int32 - Item Quantity         * Int32 - Item Quantity         * Int32 - Item Quantity         * Int32 - Item Quantity
             * byte - Amount of Seals (2)       * Byte - Amount of Seals (0)    * Byte - Amount of Seals (0)    * Byte - Amount of Seals (0)    * Byte - Amount of Seals (2)
             * byte - Seal1 Type (0) = RareOpt  * Int32 - ? (0)                 * Byte - Sockets (0)            * Byte - Sockets (0)            * Byte - Seal1 Type (0)
             * int32 - RareOptionID             *                               * Byte - Sockets Used (0)       * Byte - Sockets Used (0)       * Int32 - RareOptionID
             * int32 - No Idea                  *                               * Int32 - ? (0)                 * Int32 - ? (0)                 * Byte - ? (0)
             * Byte - RareOptionValue           *                               *                               *                               * Int32 - RareOptionValue
             * int32 - ? 0                      *                               *                               *                               * Byte - ? (0)
             * Byte - Sockets (3)      <--+     *                               *                               *                               * Int32 - ? (0)
             * Byte - Sockets Used (1)    |     *                               *                               *                               *
             * Int32 - Socket Stone ID   -+     *                               *                               *                               *
             * Int32 - Socket Stone ID   -+     *                               *                               *                               *
             * Int32 - Socket Stone ID   -+     *                               *                               *                               *
             * 
             */




            MemoryStream td = new MemoryStream();
            td.AddByte(0x0a);
            td.AddByte(0x00);
            td.AddByte(Type);
            td.AddByte(InventoryRow);


            return td;
        }

        public static MemoryStream CastleSiegeTimeAnnounce()
        {
            int CastleZone = 7; //Dratan
            byte Month = 5;
            byte Day = 29;
            byte Hour = 20;
            byte Minute = 0;

            MemoryStream td = new MemoryStream();
            td.AddByte(0x2a);
            td.AddByte(0x20);
            td.AddInt32(CastleZone);
            td.AddByte(Month);
            td.AddByte(Day);
            td.AddByte(Hour);
            td.AddByte(Minute);

            return td;

        }

        public static MemoryStream SpawnNPC(int NPCid , float NPCPosX , float NPCPosZ , float NPCPosH , float NPCPosR)
        {

            int NoIdea1 = 241589;
            byte NoIdea2 = 0;
            int NPCCurHP = 1000;
            int NPCMaxHP = 1000;
            int NPCCurMP = 0;
            int NPCMaxMP = 0;
            Int16 NoIdea3 = 0;
            int NoIdea4 = -1;
            int NoIdea5 = 0;

            MemoryStream td = new MemoryStream();
            td.AddByte(0x07);
            td.AddByte(0x00);
            td.AddByte(0x01);
            td.AddInt32(NoIdea1);
            td.AddInt32(NPCid);
            td.AddFloat(NPCPosX);
            td.AddFloat(NPCPosZ);
            td.AddFloat(NPCPosH);
            td.AddFloat(NPCPosR);
            td.AddByte(NoIdea2);
            td.AddInt32(NPCCurHP);
            td.AddInt32(NPCMaxHP);
            td.AddInt32(NPCCurMP);
            td.AddInt32(NPCMaxMP);
            td.AddInt16(NoIdea3);
            td.AddInt32(NoIdea4);
            td.AddInt32(NoIdea5);
            td.AddByte(0x00);
            td.AddByte(0x00);
            td.AddByte(0x00);
            td.AddByte(0x00);


            return td;

        }
    }
}
